package com.gulimall.seckill.scheduled;

import com.gulimall.seckill.service.impl.SeckillServiceImpl;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @Author Rookie-6688
 * @Description 秒杀的定时任务配置
 * @Create 2021-05-13 9:38
 */
@Component
public class SeckillScheduled {

    @Resource
    private SeckillServiceImpl seckillServiceImpl;
    @Resource
    private RedissonClient redissonClient;
    //秒杀商品上架功能的锁
    private final String upload_lock = "seckill:upload:lock";

    @Scheduled(cron = "*/3 * * * * ?")        // 每天的三点整触发
    public void uploadSeckillSkuLasted3Days(){
        // 加锁保证线程安全
        RLock lock = redissonClient.getLock(upload_lock);
        try {
            lock.lock(10, TimeUnit.SECONDS);        // 十秒钟没有执行完成就解锁
            seckillServiceImpl.uploadSeckillSkuLasted3Days();
        } finally {
            lock.unlock();
        }
    }
}
